﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;

namespace CommonLib
{
    public class DataSetToList
    {
        public static List<T> DsToEntityList<T>(T entity, DataSet ds) where T : new()
        {
            List<T> entityT = new List<T>();           
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                T ItemModel = Activator.CreateInstance<T>();
                Type type = ItemModel.GetType();
                PropertyInfo[] propertyInfo = type.GetProperties();
                foreach (DataColumn col in ds.Tables[0].Columns)
                {

                    for (int i = 0; i <= propertyInfo.Length - 1; i++)
                    {
                        if (col.ColumnName.ToString() == propertyInfo[i].Name)
                        {
                            if (propertyInfo[i].PropertyType.Equals(typeof(string)))
                            {
                                if (!string.IsNullOrEmpty(row[propertyInfo[i].Name].ToString()))
                                {
                                    propertyInfo[i].SetValue(ItemModel, row[propertyInfo[i].Name].ToString(), null);
                                }
                            }
                            if (propertyInfo[i].PropertyType.Equals(typeof(int)))
                            {
                                if (!string.IsNullOrEmpty(row[propertyInfo[i].Name].ToString()))
                                {
                                    propertyInfo[i].SetValue(ItemModel, int.Parse(row[propertyInfo[i].Name].ToString()), null);
                                }
                            }
                            if (propertyInfo[i].PropertyType.Equals(typeof(bool)))
                            {
                                if (!string.IsNullOrEmpty(row[propertyInfo[i].Name].ToString()))
                                {
                                    propertyInfo[i].SetValue(ItemModel, bool.Parse(row[propertyInfo[i].Name].ToString()), null);
                                }
                            }
                            if (propertyInfo[i].PropertyType.Equals(typeof(DateTime)))
                            {
                                if (!string.IsNullOrEmpty(row[propertyInfo[i].Name].ToString()))
                                {
                                    propertyInfo[i].SetValue(ItemModel, DateTime.Parse(row[propertyInfo[i].Name].ToString()), null);
                                }
                            }
                            if (propertyInfo[i].PropertyType.Equals(typeof(byte)))
                            {
                                if (!string.IsNullOrEmpty(row[propertyInfo[i].Name].ToString()))
                                {
                                    propertyInfo[i].SetValue(ItemModel, byte.Parse(row[propertyInfo[i].Name].ToString()), null);
                                }
                            }
                            if (propertyInfo[i].PropertyType.Equals(typeof(float)))
                            {
                                if (!string.IsNullOrEmpty(row[propertyInfo[i].Name].ToString()))
                                {
                                    propertyInfo[i].SetValue(ItemModel, float.Parse(row[propertyInfo[i].Name].ToString()), null);
                                }
                            }
                            if (propertyInfo[i].PropertyType.Equals(typeof(Nullable<float>)))
                            {
                                if (!string.IsNullOrEmpty(row[propertyInfo[i].Name].ToString()))
                                {
                                    propertyInfo[i].SetValue(ItemModel, float.Parse(row[propertyInfo[i].Name].ToString()), null);
                                }
                            }
                            if (propertyInfo[i].PropertyType.Equals(typeof(Nullable<int>)))
                            {
                                if (!string.IsNullOrEmpty(row[propertyInfo[i].Name].ToString()))
                                {
                                    propertyInfo[i].SetValue(ItemModel, int.Parse(row[propertyInfo[i].Name].ToString()), null);
                                }
                            }
                            if (propertyInfo[i].PropertyType.Equals(typeof(Nullable<DateTime>)))
                            {
                                if (!string.IsNullOrEmpty(row[propertyInfo[i].Name].ToString()))
                                {
                                    propertyInfo[i].SetValue(ItemModel, DateTime.Parse(row[propertyInfo[i].Name].ToString()), null);
                                }
                            }
                            if (propertyInfo[i].PropertyType.Equals(typeof(Nullable<bool>)))
                            {
                                if (!string.IsNullOrEmpty(row[propertyInfo[i].Name].ToString()))
                                {
                                    propertyInfo[i].SetValue(ItemModel, bool.Parse(row[propertyInfo[i].Name].ToString()), null);
                                }
                            }
                            if (propertyInfo[i].PropertyType.Equals(typeof(Nullable<decimal>)))
                            {
                                if (!string.IsNullOrEmpty(row[propertyInfo[i].Name].ToString()))
                                {
                                    propertyInfo[i].SetValue(ItemModel, decimal.Parse(row[propertyInfo[i].Name].ToString()), null);
                                }
                            }
                            if (propertyInfo[i].PropertyType.Equals(typeof(decimal)))
                            {
                                if (!string.IsNullOrEmpty(row[propertyInfo[i].Name].ToString()))
                                {
                                    propertyInfo[i].SetValue(ItemModel, decimal.Parse(row[propertyInfo[i].Name].ToString()), null);
                                }
                            }
                            //if (propertyInfo[i].PropertyType.Equals(typeof(Nullable<Guid>)))
                            //{
                            //    if (!string.IsNullOrEmpty(row[propertyInfo[i].Name].ToString()))
                            //    {
                            //        propertyInfo[i].SetValue(ItemModel, Guid.Parse(row[propertyInfo[i].Name].ToString()), null);
                            //    }
                            //}

                        }

                    }
                 
                }
                entityT.Add(ItemModel);
            }
            return entityT;
        }
    }
}
